//
//  MyViewController.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/2.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "MyViewController.h"
#import "UserDetailInfoViewController.h"
#import "VolunteerRecordViewController.h"
#import "ANScoreViewController.h"
#import "SetingViewController.h"
#import "AddressListViewController.h"
#import "ModelUser.h"
#import "UserInfoManager.h"
#import "UIImageView+WebCache.h"
#import "StarViewController.h"
#import "ZBarCardViewController.h"
#import "AdScrollView.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)UIView *headView;
@property (strong, nonatomic)UIImageView *logoImageView;
@property (strong, nonatomic)UILabel *telLabel;
@property (strong, nonatomic)UILabel *serverTimeLabel;
@property (strong, nonatomic)UITableView *tableView;
@property(strong,nonatomic)NSArray *starArr;
@property(strong,nonatomic)UILabel *starNameLabel;
@property(strong,nonatomic)UIButton *scoreButton;
@property(strong,nonatomic)UILabel *statusLabel;
@property(strong,nonatomic)UIImageView *statusImageView;

@property (strong, nonatomic)UIButton *exitButton;

@property(strong,nonatomic)NSArray *data;


@property(strong,nonatomic)AdScrollView *adScrollView;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我";
    
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(5, 20+44+5, SCREEN_WIDTH-10, (SCREEN_HEIGHT-20)/7)];
    view1.backgroundColor = [UIColor colorWithRed:0.972549F green:0.972549F blue:0.972549F alpha:1.0F];
    view1.layer.cornerRadius =5;
    [self.view addSubview:view1];
    self.headView=view1;
    //
    ModelUser *modeluser = [UserInfoManager sharedManager].modelMser;
    UIImageView *touxiangIV = [[UIImageView alloc]init];
    touxiangIV.frame =CGRectMake(5, 5, view1.height-10, view1.height-10);
    touxiangIV.layer.cornerRadius = 5;
    touxiangIV.layer.masksToBounds =YES;
    touxiangIV.image = [UIImage imageNamed:@"man"];
    [view1 addSubview:touxiangIV];
    self.logoImageView=touxiangIV;
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:modeluser.headImageUrl] placeholderImage:nil];
    UILabel *phoneNUm = [[UILabel alloc]initWithFrame:CGRectMake(touxiangIV.x+touxiangIV.width, 0, SCREEN_WIDTH*2/3, (SCREEN_HEIGHT-20)/21)];
    //
    self.telLabel=phoneNUm;
//    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
//    NSString *str=[defaults objectForKey:@"phoneNUm"];
    NSString *text;
    if(modeluser.loginMobile.length==0){
        text=@"";
    }else{
        text=modeluser.loginMobile;
    }
    phoneNUm.text =[NSString stringWithFormat:@"手机号码:%@",text];
    self.telLabel=phoneNUm;
    
    // phoneNUm.backgroundColor = [UIColor grayColor];
    if(SCREEN_HEIGHT>568){
        phoneNUm.font=[UIFont systemFontOfSize:17.0f];
    }else{
        phoneNUm.font = [UIFont systemFontOfSize:15.0f];
    }
    [view1 addSubview:phoneNUm];
    UILabel *serverNUm = [[UILabel alloc]initWithFrame:CGRectMake(touxiangIV.x+touxiangIV.width, (view1.height-(SCREEN_HEIGHT-20)/21)/2, SCREEN_WIDTH*2/3, (SCREEN_HEIGHT-20)/21)];
    
    serverNUm.text =[NSString stringWithFormat:@"服务时长:%d小时",[modeluser.voluteerServiceHours intValue]];
    //serverNUm.backgroundColor = [UIColor grayColor];
    if(SCREEN_HEIGHT>568){
        serverNUm.font=[UIFont systemFontOfSize:17.0f];
    }else{
        serverNUm.font = [UIFont systemFontOfSize:15.0f];
    }
    self.serverTimeLabel=serverNUm;
    serverNUm.textAlignment =4;
    [view1 addSubview:serverNUm];
    //箭头
    UIButton *arrowsBtn =[[UIButton alloc]initWithFrame:CGRectMake(view1.width-20, (view1.height-30)/2, 20, 30)];
    [arrowsBtn setImage:[UIImage imageNamed:@"xjt_3"] forState:UIControlStateNormal];
    [arrowsBtn addTarget:self action:@selector(chickDetailButton:) forControlEvents:UIControlEventTouchUpInside];
//    [arrowsBtn addTarget:self action:@selector(personInfoBtn) forControlEvents:UIControlEventTouchUpInside];
    //arrowsBtn.backgroundColor =[UIColor redColor];
    [view1 addSubview:arrowsBtn];
    //星级
    UILabel *StarsLab = [[UILabel alloc]initWithFrame:CGRectMake(touxiangIV.x+touxiangIV.width,view1.height-(SCREEN_HEIGHT-20)/21, SCREEN_WIDTH/8, (SCREEN_HEIGHT-20)/21)];
    StarsLab.text =@"星级:";
    if(SCREEN_HEIGHT>568){
        StarsLab.font=[UIFont systemFontOfSize:17.0f];
    }else{
        StarsLab.font = [UIFont systemFontOfSize:15.0f];
    }
    StarsLab.textAlignment =0;
    [view1 addSubview:StarsLab];
    
    int startNum=[modeluser.voluteerLevel intValue];
    NSMutableArray *arrM=[NSMutableArray array];
    for (int i =0; i<5; i++) {
        //
        UIButton *starBtn = [[UIButton alloc]initWithFrame:CGRectMake(StarsLab.x+StarsLab.width+i*SCREEN_WIDTH/25-5, 2*SCREEN_HEIGHT/21+SCREEN_HEIGHT/(21*2)-SCREEN_WIDTH/(25*2), SCREEN_WIDTH/25, SCREEN_WIDTH/25)];
        starBtn.center=CGPointMake(starBtn.center.x, StarsLab.center.y);
        if (i<startNum) {
            [starBtn setImage:[UIImage imageNamed:@"pentagram2"] forState:UIControlStateNormal];
        }else{
            [starBtn setImage:[UIImage imageNamed:@"pentagram1"] forState:UIControlStateNormal];
        }
        [starBtn addTarget:self action:@selector(chickStarButton:) forControlEvents:UIControlEventTouchUpInside];
        [view1 addSubview:starBtn];
        [arrM addObject:starBtn];
    }
    self.starArr=arrM;
    
    //几星级
    UIButton *lastButton=[self.starArr lastObject];
    UILabel *StarsLab1 = [[UILabel alloc]initWithFrame:CGRectMake(lastButton.x+lastButton.width+10, 2*SCREEN_HEIGHT/21, SCREEN_WIDTH/5, SCREEN_HEIGHT/21)];
    StarsLab1.text =[NSString stringWithFormat:@"%d星级志愿者",[modeluser.voluteerLevel intValue]];
    if(SCREEN_HEIGHT>568){
        StarsLab1.font=[UIFont systemFontOfSize:13.0f];
    }else{
        StarsLab1.font = [UIFont systemFontOfSize:11.0f];
    }
    StarsLab1.textAlignment =0;
    [view1 addSubview:StarsLab1];
    self.starNameLabel=StarsLab1;
    //问号
    UIButton *explainBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    // explainBtn.frame =CGRectMake(SCR_HEIGHT/7+SCR_WIDTH/8+6*SCR_WIDTH/25+SCR_WIDTH/5, 2*SCR_HEIGHT/21+SCR_HEIGHT/(21*2)-SCR_HEIGHT/(35*2), SCR_HEIGHT/35, SCR_HEIGHT/35);
    explainBtn.frame =CGRectMake(SCREEN_WIDTH/4+SCREEN_WIDTH/8+6*SCREEN_WIDTH/25+SCREEN_WIDTH/5-10, 2*SCREEN_HEIGHT/21+SCREEN_HEIGHT/(21*2)-SCREEN_HEIGHT/(25*2), SCREEN_WIDTH/25, SCREEN_WIDTH/25);
    [explainBtn setImage:[UIImage imageNamed:@"question"] forState:UIControlStateNormal];
    [explainBtn addTarget:self action:@selector(chickQuestionButton:) forControlEvents:UIControlEventTouchUpInside];
//    [explainBtn addTarget:self action:@selector(toScoringVC) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:explainBtn];
    //积分
    UIButton *integralBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    integralBtn.frame =CGRectMake(SCREEN_WIDTH*6/7-10, 2*SCREEN_HEIGHT/21, SCREEN_WIDTH/7, SCREEN_HEIGHT/21);
    
    NSString *tempStr=[NSString stringWithFormat:@"积分:%d",[modeluser.ANscore intValue]];
    [integralBtn setTitle:tempStr forState:UIControlStateNormal];
    [integralBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    if(SCREEN_HEIGHT>568){
        integralBtn.titleLabel.font=[UIFont systemFontOfSize:14.0f];
    }else{
        integralBtn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
    }
    self.scoreButton=integralBtn;
    integralBtn.titleLabel.textAlignment =NSTextAlignmentLeft;
    //integralBtn.backgroundColor =[UIColor blueColor];
    [integralBtn addTarget:self action:@selector(chickScoreButton:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:integralBtn];
    explainBtn.center=CGPointMake(explainBtn.center.x, integralBtn.center.y);
    
    //审核状态
    UILabel *statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*4/5, 0, SCREEN_WIDTH/5, SCREEN_HEIGHT/21)];
    statusLabel.text =@"认证中";
    //auditStatus.backgroundColor = [UIColor grayColor];
    statusLabel.textColor =[UIColor redColor];
    statusLabel.font = [UIFont systemFontOfSize:12];
    statusLabel.textAlignment =NSTextAlignmentCenter;
    [view1 addSubview:statusLabel];
    self.statusLabel=statusLabel;
    
    UIImageView *statusImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20,20)];
    statusImageView.image=[UIImage imageNamed:@"v.png"];
    statusImageView.center=statusLabel.center;
    [view1 addSubview:statusImageView];
    self.statusImageView=statusImageView;
    
    if([modeluser.status intValue]==0){
        statusLabel.hidden=YES;
        statusImageView.hidden=NO;

    }else{
        statusLabel.hidden=NO;
        statusImageView.hidden=YES;
    }
    
    //
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(5, view1.y+view1.height+5, SCREEN_WIDTH-10, (SCREEN_HEIGHT-20)/7)];
    view2.backgroundColor = [UIColor whiteColor];
    //    view2.layer.borderWidth=1;
    //    view2.layer.borderColor =[UIColor blackColor].CGColor;
    view2.layer.masksToBounds = YES;
    view2.layer.cornerRadius =5;
    [self.view addSubview:view2];
    
    //i6以上适配
    if(SCREEN_HEIGHT>568){
        view2.height=(SCREEN_HEIGHT-64-49-5*5-view1.height)/4;
    }
    //广告页
    AdScrollView *adScrollView=[[AdScrollView alloc]initWithFrame:CGRectMake(0, 0, view2.width, view2.height)];
    self.adScrollView=adScrollView;
    [view2 addSubview:adScrollView];
    self.adScrollView.layer.cornerRadius=5.0f;
    NSArray *data=@[@"guanggao_4.jpg",@"guanggao_0.jpg",@"guanggao_1.jpg",@"guanggao_2.jpg",@"guanggao_3.jpg",@"guanggao_4.jpg",@"guanggao_0.jpg"];
    self.adScrollView.data=data;
    
    UIButton *exitButton=[UIButton buttonWithType:UIButtonTypeCustom];
    exitButton.titleLabel.font = [UIFont systemFontOfSize:14];
    exitButton.frame=CGRectMake(5, SCREEN_HEIGHT-49-5-40, SCREEN_WIDTH-10, 40);
    if(SCREEN_HEIGHT>480){
        
    }else{
        exitButton.height=25;
        exitButton.y=SCREEN_HEIGHT-49-5-25;
    }
    exitButton.backgroundColor=[UIColor redColor];
    [exitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [exitButton setTitle:@"退出" forState:UIControlStateNormal];
    exitButton.layer.cornerRadius =5;
    [self.view addSubview:exitButton];
    [exitButton addTarget:self action:@selector(chickExitButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(5, view2.y+view2.height+5,SCREEN_WIDTH-10, (SCREEN_HEIGHT-20)*6/15)];
    if (SCREEN_HEIGHT>480) {
        self.tableView.frame =CGRectMake(5, view2.y+view2.height+5, SCREEN_WIDTH-10, (SCREEN_HEIGHT-64-49-5*5-self.headView.height-self.adScrollView.height-40));
    }
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    self.tableView.scrollEnabled =NO;
    self.tableView.layer.cornerRadius =5;
    self.tableView.layer.borderWidth=0.5;
    self.tableView.layer.borderColor=[UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1].CGColor;
    [self.view addSubview:self.tableView];

    self.data=@[@"志愿记录",@"服务积分",@"二维码名片",@"扫一扫",@"通讯录",@"设置"];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellIdentifier"];
    
}


//更新数据
-(void)refreshData{
    ModelUser *modeluser = [UserInfoManager sharedManager].modelMser;
    //头像
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:modeluser.headImageUrl] placeholderImage:nil];
    //手机号码
    NSString *text;
    if(modeluser.loginMobile.length==0){
        text=@"";
    }else{
        text=modeluser.loginMobile;
    }
    self.telLabel.text =[NSString stringWithFormat:@"手机号码:%@",text];
    //认证
    if([modeluser.status intValue]==0){
        self.statusLabel.hidden=NO;
        self.statusImageView.hidden=YES;
    }else{
        self.statusLabel.hidden=YES;
        self.statusImageView.hidden=NO;
    }
    //服务时长
    self.serverTimeLabel.text =[NSString stringWithFormat:@"服务时长:%d小时",[modeluser.voluteerServiceHours intValue]];
    //星级
    [self starNumWithNum:[modeluser.voluteerLevel intValue]];
    //A/N星级志愿
    self.starNameLabel.text =[NSString stringWithFormat:@"%d星级志愿者",[modeluser.voluteerLevel intValue]];
    //积分
    NSString *tempStr=[NSString stringWithFormat:@"积分:%d",[modeluser.ANscore intValue]];
    [self.scoreButton setTitle:tempStr forState:UIControlStateNormal];
}

-(void)starNumWithNum:(int)num{
    if(self.starArr==nil){
        
    }else{
        for (int i =0; i<self.starArr.count; i++) {
            //
            UIButton *starBtn=self.starArr[i];
            if (i<num) {
                [starBtn setImage:[UIImage imageNamed:@"pentagram2"] forState:UIControlStateNormal];
            }else{
                [starBtn setImage:[UIImage imageNamed:@"pentagram1"] forState:UIControlStateNormal];
            }
        }

    }
}

- (void)chickExitButton:(UIButton *)sender {
    //退出
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(SCREEN_HEIGHT>480){
        return (SCREEN_HEIGHT-64-49-5*5-self.headView.height-self.adScrollView.height-40)/6;
    }else{
        return (SCREEN_HEIGHT-20)/15;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:{
            //志愿记录
            VolunteerRecordViewController *vc=[[VolunteerRecordViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:{
            //服务积分
            ANScoreViewController *vc=[[ANScoreViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:{
            //二维码名片
            ZBarCardViewController *zbarCardVC = [[ZBarCardViewController alloc]initWithNibName:@"ZBarCardViewController" bundle:nil];
            [self.navigationController pushViewController:zbarCardVC animated:YES];
        }
            break;
        case 3:{
            //扫一扫
            [self showQz:MyZbarStyle];
            [self getZBarPushBlockStr:^(id result) {
                [SVProgressHUD showInfoWithStatus:result maskType:SVProgressHUDMaskTypeBlack];
            }];
            
        }
            break;
        case 4:{
            //通讯录
            AddressListViewController *vc=[[AddressListViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 5:{
            //设置
            SetingViewController *vc=[[SetingViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"CellIdentifier";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text=self.data[indexPath.row];
    if(SCREEN_HEIGHT>568){
        cell.textLabel.font=[UIFont systemFontOfSize:19.0f];
    }else{
        cell.textLabel.font=[UIFont systemFontOfSize:15.0f];
    }
    return cell;
}

#pragma mark - headView
-(void)chickDetailButton:(UIButton *)sender{
    //详细
    UserDetailInfoViewController *vc=[[UserDetailInfoViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)chickQuestionButton:(UIButton *)sender{
    //按动问号
    ANScoreViewController *vc=[[ANScoreViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)chickScoreButton:(UIButton *)sender{
    //积分按钮
    ANScoreViewController *vc=[[ANScoreViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)chickStarButton:(UIButton *)sender{
    //按动星星
//    StarViewController *vc=[[StarViewController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
}
@end
